package com.zxl.yjb.activity.my;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.zxl.yjb.R;
import com.zxl.yjb.adapter.MyFriendsListAdapter;
import com.zxl.yjb.base.BaseActivity;
import com.zxl.yjb.bean.UserBean;
import com.zxl.yjb.http.manager.DataManager;
import com.zxl.yjb.http.observer.ObserverOnNextListener;
import com.zxl.yjb.http.observer.ProgressObserver;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class BlacklistActivity extends BaseActivity implements OnRefreshLoadMoreListener {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_page_title)
    TextView tvPageTitle;
    @BindView(R.id.rv_friends_list)
    RecyclerView rvFriendsList;
    @BindView(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;
    private int currentPager = 1;
    private int pageSize = 20;
    private MyFriendsListAdapter adapter;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_blacklist);
        ButterKnife.bind(this);
        initView();
        initEvent();
        getBlacksList(false);
    }

    @Override
    protected void initView() {
        tvPageTitle.setText("黑名单");
        initAdapter();
        refreshLayout.setOnRefreshLoadMoreListener(this);
    }

    private void initAdapter() {
        //创建布局管理
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvFriendsList.setLayoutManager(layoutManager);
        //创建适配器
        adapter = new MyFriendsListAdapter(R.layout.item_my_friends, "black_list");
        //给RecyclerView设置适配器
        rvFriendsList.setAdapter(adapter);
    }


    private void initEvent() {

        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.btn_delete:
                        String userId = ((UserBean) adapter.getItem(position)).getUid()+"";
                        adapter.remove(position);
                        deleteBlackList(userId);
                        break;
                }
            }
        });

    }

    /**
     * 获取好友黑名单列表
     */
    private void getBlacksList(final boolean isRefresh) {

        Map<String, String> map = new HashMap<>();
        map.put("page", currentPager+"");
        map.put("count", pageSize+"");
        map.put("attribute", "2");
        DataManager dataManager = new DataManager();
        ObserverOnNextListener<List<UserBean>> listener = new ObserverOnNextListener<List<UserBean>>() {
            @Override
            public void onNext(List<UserBean> detailBeanList) {
                currentPager++;
                if (isRefresh) {
                    adapter.setNewData(detailBeanList);
                    refreshLayout.finishRefresh();
                }else {
                    adapter.addData(detailBeanList);
                    refreshLayout.finishLoadMore();
                    if (detailBeanList.size()<pageSize){
                        refreshLayout.finishLoadMoreWithNoMoreData();//将不会再次触发加载更多事件
                    }
                }
            }
        };
        dataManager.friendsList(new ProgressObserver<List<UserBean>>(listener,this){
            @Override
            public void showDialog() {
            }
        },map);

    }


    /**
     * 移除好友黑名单列表
     */
    private void deleteBlackList(String id) {
        DataManager dataManager = new DataManager();
        ObserverOnNextListener<Object> listener = new ObserverOnNextListener<Object>() {
            @Override
            public void onNext(Object data) {

            }
        };
        dataManager.deletefriend(new ProgressObserver<Object>(listener,this),id);
    }

    @OnClick(R.id.iv_back)
    public void onViewClicked() {
        finish();
    }



    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        refreshLayout.finishLoadMore(5000);
        getBlacksList(false);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        refreshLayout.finishRefresh(5000);
        currentPager=1;
        getBlacksList(true);
        refreshLayout.setNoMoreData(false);
    }
}
